﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using CIRDiagnoseDevice.DBManage;

namespace CIRDiagnoseDevice
{
    public delegate void FormDelegate(object sender);
    public partial class Statistics : Form
    {
        public event FormDelegate ShowForm;
        private string StartTime;
        private string EndTime;
        private string ModType;
        TestRecordManage manage;
        public Statistics()
        {
            InitializeComponent();
            manage = new TestRecordManage();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (StartTime == EndTime)
            {
                MessageBox.Show("统计起始时间与终止时间不能相同！");
            }
            else if (comboBoxEdit1.Text == "")
            {
                MessageBox.Show("请选择统计模块!");
            }
            else
            {
                DataTable dt = GetStatisticData();
                if (dt.Rows.Count == 0 || dt == null)
                {
                    MessageBox.Show("无相关统计信息!");
                }
                else
                {
                    CreateSeries(dt);
                }
            }
        }
     
        private DataTable GetStatisticData()
        {
            string sql = "select 模块名称,测试结果,count(模块名称) as '数量' ,deviceInfo.CIR生产商 from test_rec, deviceInfo where 测试时间 between '" +StartTime +"' and ' " +EndTime +
                                "' and 模块名称 like'%" + ModType + "%'and test_rec.设备编号 = deviceInfo.CIR编号 group by CIR生产商,测试结果 , 模块名称";
            DataTable dt = manage.QueryData(sql);
            return dt;
        }

    
        private void CreateSeries(DataTable dt)
        {
            try
            {
                double total = 0;
                DataTable unqualifiedDb = new DataTable();
                DataTable qualifiedDb = new DataTable();
                DataTable qualifiedRateDb = new DataTable();
                DataColumn col = new DataColumn("数量");
                DataColumn col2 = new DataColumn("生产商");
                DataColumn col3 = new DataColumn("数量");
                DataColumn col4 = new DataColumn("生产商");
                DataColumn col5 = new DataColumn("数量");
                DataColumn col6 = new DataColumn("生产商");
                unqualifiedDb.Columns.Add(col);
                unqualifiedDb.Columns.Add(col2);
                qualifiedDb.Columns.Add(col3);
                qualifiedDb.Columns.Add(col4);
                qualifiedRateDb.Columns.Add(col5);
                qualifiedRateDb.Columns.Add(col6);
                if (StatisticChart.Series != null)
                {
                    StatisticChart.Series.Clear();
                }
                if (chartControl1.Series != null)
                {
                    chartControl1.Series.Clear();
                }
                Series series = new Series("合格", ViewType.Bar);
                Series series2 = new Series("不合格", ViewType.Bar);
                Series series3 = new Series("合格率", ViewType.Bar);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["测试结果"].ToString() == "合格")
                    {
                        DataRow row = qualifiedDb.NewRow();
                        row[0] = dt.Rows[i]["数量"];
                        row[1] = dt.Rows[i]["CIR生产商"];
                        qualifiedDb.Rows.Add(row);
                    }
                    else if (dt.Rows[i]["测试结果"].ToString() == "不合格")
                    {
                        DataRow row = unqualifiedDb.NewRow();
                        row[0] = dt.Rows[i]["数量"];
                        row[1] = dt.Rows[i]["CIR生产商"];
                        unqualifiedDb.Rows.Add(row);
                    }
                }
                for (int i = 0; i < qualifiedDb.Rows.Count; i++)
                {
                    SeriesPoint point = new SeriesPoint(qualifiedDb.Rows[i][1].ToString(), Convert.ToDouble(qualifiedDb.Rows[i][0].ToString()));
                    series.Points.Add(point);
                }
                for (int i = 0; i < unqualifiedDb.Rows.Count; i++)
                {
                    SeriesPoint point = new SeriesPoint(unqualifiedDb.Rows[i][1].ToString(), Convert.ToDouble(unqualifiedDb.Rows[i][0].ToString()));
                    series2.Points.Add(point);
                }
                if (dt.Rows.Count > 1)
                {
                    for (int i = 0; i < dt.Rows.Count - 1; i++)
                    {

                        if (dt.Rows[i]["CIR生产商"].ToString() == dt.Rows[i + 1]["CIR生产商"].ToString())
                        {
                            if (dt.Rows[i]["测试结果"].ToString() == "--")
                            {
                                total = Convert.ToDouble(dt.Rows[i + 1]["数量"].ToString());
                            }
                            else if (dt.Rows[i + 1]["测试结果"].ToString() == "--")
                            {
                                Convert.ToDouble(dt.Rows[i]["数量"].ToString());
                            }
                            else
                            {
                                total = Convert.ToDouble(dt.Rows[i]["数量"].ToString()) + Convert.ToDouble(dt.Rows[i + 1]["数量"].ToString());
                            }
                            DataRow row = qualifiedRateDb.NewRow();
                            row[0] = total;
                            row[1] = dt.Rows[i]["CIR生产商"].ToString();
                            qualifiedRateDb.Rows.Add(row);
                            i++;
                        }
                        else
                        {
                            if (i == dt.Rows.Count - 1)
                            {
                                total = Convert.ToDouble(dt.Rows[i]["数量"].ToString());
                                DataRow row = qualifiedRateDb.NewRow();
                                row[0] = total;
                                row[1] = dt.Rows[i]["CIR生产商"].ToString();
                                qualifiedRateDb.Rows.Add(row);
                                total = Convert.ToDouble(dt.Rows[i + 1]["数量"].ToString());
                                DataRow row1 = qualifiedRateDb.NewRow();
                                row1[0] = total;
                                row1[1] = dt.Rows[i + 1]["CIR生产商"].ToString();
                                qualifiedRateDb.Rows.Add(row1);
                            }
                            else
                            {
                                total = Convert.ToDouble(dt.Rows[i]["数量"].ToString());
                                DataRow row = qualifiedRateDb.NewRow();
                                row[0] = total;
                                row[1] = dt.Rows[i]["CIR生产商"].ToString();
                                qualifiedRateDb.Rows.Add(row);

                                total = Convert.ToDouble(dt.Rows[i + 1]["数量"].ToString());
                                DataRow row2 = qualifiedRateDb.NewRow();
                                row2[0] = total;
                                row2[1] = dt.Rows[i + 1]["CIR生产商"].ToString();
                                qualifiedRateDb.Rows.Add(row2);
                            }
                        }
                    }
                }
                else
                {
                    total = Convert.ToDouble(dt.Rows[0]["数量"].ToString());
                    DataRow row = qualifiedRateDb.NewRow();
                    row[0] = total;
                    row[1] = dt.Rows[0]["CIR生产商"].ToString();
                    qualifiedRateDb.Rows.Add(row);
                }
                int j =0;
                for (int i = 0; i < qualifiedRateDb.Rows.Count; i++)
                {
                    if (qualifiedDb.Rows[j][1].ToString() == qualifiedRateDb.Rows[i][1].ToString())
                    {
                        double value = Convert.ToDouble(qualifiedDb.Rows[i][0].ToString()) / Convert.ToDouble(qualifiedRateDb.Rows[i][0].ToString());
                        SeriesPoint point = new SeriesPoint(qualifiedRateDb.Rows[i][1].ToString(), value);
                        series3.Points.Add(point);
                        j++;
                    }
                    else
                    {
                        double value = 0 / Convert.ToDouble(qualifiedRateDb.Rows[i][0].ToString());
                        SeriesPoint point = new SeriesPoint(qualifiedRateDb.Rows[i][1].ToString(), value);
                        series3.Points.Add(point);
                    }
                }
                StatisticChart.Series.Add(series);
                StatisticChart.Series.Add(series2);
                chartControl1.Series.Add(series3);
                series2.View.Color = Color.Red;
                series.View.Color = Color.Green;
                series3.Label.ResolveOverlappingMode = ResolveOverlappingMode.Default;
                XYDiagram diagram = StatisticChart.Diagram as XYDiagram;
                diagram.AxisX.Title.Text = "设备生产商";
                diagram.AxisX.Visible = true;
                diagram.AxisY.Title.Text = "测试情况";
                diagram.AxisY.Visible = true;
                XYDiagram diagram2 = chartControl1.Diagram as XYDiagram;
                diagram2.AxisY.Title.Text = "百分比";
                diagram2.AxisY.Visible = true;
                diagram2.AxisX.Visible = true;
                foreach (Series s in StatisticChart.Series)
                {
                    BarSeriesView bar = (BarSeriesView)s.View;
                    if (s.Name == "合格")
                    {
                        bar.FillStyle.FillMode = FillMode.Solid;
                        bar.Color = Color.Green;
                    }
                    else if (s.Name == "不合格")
                    {
                        bar.Color = Color.Red;
                        bar.FillStyle.FillMode = FillMode.Solid;
                    }
                    bar.BarWidth = 0.3;
                }
                BarSeriesView bar2 = (BarSeriesView)series3.View;
                bar2.BarWidth = 0.3;
                bar2.FillStyle.FillMode = FillMode.Solid;
                bar2.Color = Color.Green;
                ChartTitle title = new ChartTitle();
                title.Text = comboBoxEdit1.Text + "统计情况";
                title.Dock = ChartTitleDockStyle.Top;
                title.Font = new Font("微软雅黑", 16);
                if (StatisticChart.Titles.Count != 0)
                {
                    if (title.Text != StatisticChart.Titles[0].Text)
                    {
                        StatisticChart.Titles.Clear();
                        StatisticChart.Titles.Add(title);
                    }
                }
                else
                {
                    StatisticChart.Titles.Add(title);
                }
                ChartTitle title2 = new ChartTitle();
                title2.Dock = ChartTitleDockStyle.Top;
                title2.Font = new Font("微软雅黑", 16);
                title2.Text = comboBoxEdit1.Text + "合格率情况";
                if (chartControl1.Titles.Count != 0)
                {
                    if (title2.Text != chartControl1.Titles[0].Text)
                    {
                        chartControl1.Titles.Clear();
                        chartControl1.Titles.Add(title2);
                    }
                }
                else
                {
                    chartControl1.Titles.Add(title2);
                }
                diagram2.AxisX.NumericOptions.Format = NumericFormat.Percent;
                diagram2.AxisX.Title.Text = "设备生产商";
                series3.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
                StatisticChart.Invalidate();
                chartControl1.Invalidate();
            }
            catch
            {

            }
        }

        private void Statistics_Load(object sender, EventArgs e)
        {
            dateEdit1.DateTime = System.DateTime.Now;
            dateEdit2.DateTime = System.DateTime.Now;
            StartTime = dateEdit1.Text;
            EndTime = dateEdit2.Text;
        }

        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {
            DateEdit dateObj = sender as DateEdit;
            if (dateObj.Name == "dateEdit1")
            {
                StartTime = dateEdit1.Text;
            }
            else if (dateObj.Name == "dateEdit2")
            {
                EndTime = dateEdit2.Text;
            }
        }

        private void comboBoxEdit1_TextChanged(object sender, EventArgs e)
        {
            ModType = comboBoxEdit1.Text;
            //if (comboBoxEdit1.Text == "450MHz发射机")
            //    ModType = "发射机";
            //else if (comboBoxEdit1.Text == "450MHz同频接收机")
            //    ModType = "同频接收机";
            //else if (comboBoxEdit1.Text == "450MHz异频接收机")
            //    ModType = "异频接收机";
            //else if (comboBoxEdit1.Text == "450MHz信令")
            //    ModType = "信令";
            //else if (comboBoxEdit1.Text == "450MHz列尾功能试验")
            //    ModType = "列尾功能试验";
            //else if (comboBoxEdit1.Text == "GSM-R语音")
            //    ModType = comboBoxEdit1.Text;
            //else if (comboBoxEdit1.Text == "GSM-R数据")
            //    ModType = comboBoxEdit1.Text;
            //else if (comboBoxEdit1.Text == "GSM-R功能试验")
            //    ModType = comboBoxEdit1.Text;
            //else if (comboBoxEdit1.Text == "LBJ功能试验")
            //    ModType = comboBoxEdit1.Text;
            //else if (comboBoxEdit1.Text == "LBJ发射机")
            //    ModType = comboBoxEdit1.Text;
            //else if (comboBoxEdit1.Text == "LBJ同频接收机")
            //    ModType = comboBoxEdit1.Text;
            //else if (comboBoxEdit1.Text == "LBJ异频接收机")
            //    ModType = comboBoxEdit1.Text;
        }
    }
}
